# 检查特征提取
import h5py
import numpy as np
import os
import pickle

from Config.Config import DPN107_RGB_200_PATH
from Config.Config import TSNscore_FEATURE_PATH as FEATURE
from Config.Config import FRAME_COUNT_PKL

from FeatureExtruct.DPN107_RGB.RGB_SinglePicture_Dataset import RGB_Single_Frame_Dataset

TTT = 'AA6nXQMyfhU,fG0nn2IVdDM,K5wPwCFVkhU,pElJ7U46XnQ,Uofnmkfohkc,ZTwmb1d44bc,aAY_M6M26TI,fgBFlwM466w,k7nuduqYOdQ,\
Pfc7KbwqdYk,UoGoOznvKew,zuNQFkkyBNo,aB5xErksFkI,fgP2pf2rh4Q,K7oJNZsI1Cc,PG0ao4HkF8M,uOk4EFDsDP4,zUt53fHpqc8'
TTT = TTT.split(',')

vids = TTT

# TCN feature
def load_TSNscore_from_file(vid):
    with h5py.File(FEATURE + '/feat/%s.h5' % vid, 'r') as hf:
        fg = np.asarray(hf['fg'])
        bg = np.asarray(hf['bg'])
        feat1 = np.hstack([fg,bg])
    with h5py.File(FEATURE + '/flow/%s.h5' % vid, 'r') as hf:
        fg2 = np.asarray(hf['fg']) # 200
        bg2 = np.asarray(hf['bg']) # 2
        feat2 = np.hstack([fg2,bg2])
    feat = feat1 + feat2
    return feat


# dpn107 201 feature
def load_DPN107_from_file(vid):
    try:
        with h5py.File(os.path.join(DPN107_RGB_200_PATH,'{}.h5'.format(vid)),'r') as f:
            d = f['feature201'][:]
            return d
    except Exception as E:
        raise E

# frame len
with open(FRAME_COUNT_PKL,'rb') as f:
    frame_count = pickle.load(f)

keys = 'rhOtqArO-3Y'

frame_num = frame_count[keys]
tsn_feature = load_TSNscore_from_file(keys)
dpn_feature = load_DPN107_from_file(keys)

tsn_feature.shape
dpn_feature.shape

# dataset = RGB_Single_Frame_Dataset('training')
# gen = dataset.enum_all_picture()
#
# for i,X in enumerate(gen):
#     print(i,X[0],len(X[2]))

import matplotlib.pyplot as plt

for i in range(201):
    plt.clf()
    # plt.plot(tsn_feature[:,i])
    plt.plot(dpn_feature[:,i])
    plt.ylim(0,1)
    # plt.show()
    # plt.savefig('/tmp/PICTURE/TSN/tsn_{}.jpg'.format(i))
    plt.savefig('/tmp/PICTURE/DPN/dpn_{:05d}.jpg'.format(i))

from Config.Config import ACTNET200V13_PKL
import pickle
with open(ACTNET200V13_PKL,'rb') as f:
    d = pickle.load(f)
database = d['database']
database[keys]

plt.plot(tsn_feature[:,200])
plt.show()

plt.plot(tsn_feature[:,201])
plt.show()
